<template>
  <div class="coupon_list-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="margin_right_20">
          用户名称:&nbsp;
          <el-input
            v-model="opt1"
            style="width:100px;height:30px;"
            @keyup.enter.native="get_list(1)"
          ></el-input>
        </div>
        <div class="margin_right_20">
          用户ID:&nbsp;
          <el-input
            v-model="opt1"
            style="width:100px;height:30px;"
            @keyup.enter.native="get_list(1)"
          ></el-input>
        </div>

        <div class="margin_right_20">
          账号状态:&nbsp;
          <el-select v-model="opt3" style="width: 100px;">
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>

        <div class="btn">
          <el-button type="primary" icon="el-icon-search" @click="get_list(1)">查询</el-button>
        </div>
      </div>
      <div class="table">
        <el-table :data="couponTypeList" style="width: 100%" height="598">
          <el-table-column prop="type" label="用户ID"></el-table-column>
          <el-table-column prop="createtime" label="用户昵称"></el-table-column>
          <el-table-column prop="note" label="已使用优惠价（张）"></el-table-column>
          <el-table-column prop="note" label="未使用优惠价（张）"></el-table-column>
          <el-table-column prop="note" label="已过期优惠价（张）"></el-table-column>
        </el-table>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";

import { coupon_list, coupon_delete } from "../../api/getData.js";
export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      form: {},
      //控制添加对话框的显示和隐藏
      dialogVisible: false,
      currentPage: 1,
      total: 0,
      page: 1,
      pageSize: 10,
      opt1: "",
      opt2: "",
      opt3: "",
      // value4: "",
      // value5: "",
      options3: [
        { id: "", label: "全部" },
        { id: "1", label: "可使用" },
        { id: "2", label: "禁用" },
        { id: "3", label: "已过期" }
      ],
      options2: [
        { id: "", label: "全部" },
        { id: "1", label: "全场通用" },
        { id: "2", label: "部分活动通用" },
        { id: "3", label: "部分商品类型通用" },
        { id: "4", label: "部分活动商品类型通用" },
        { id: "5", label: "部分商品可用" }
      ],
      couponList: [],
      couponMsg: { name: "", id: "" }
    };
  },
  created() {
    this.get_list();
  },
  methods: {
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_list();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_list();
    },
    jump_detail(id) {
      this.$router.push("/coupon_detail?id=" + id);
    },
    jump_modify(id) {
      this.$router.push("/coupon_modify?id=" + id);
    },
    get_list() {
      // alert(this.$store.getters.txtData)
    },
    // get_list(currentPage) {
    //   // let data =
    //   //   "name=" +
    //   //   this.opt1 +
    //   //   "&type=" +
    //   //   this.opt2 +
    //   //   "&source=" +
    //   //   this.value3 +
    //   //   "&startTime=" +
    //   //   this.value4 +
    //   //   "&endTime=" +
    //   //   this.value5 +
    //   //   "&size=" +
    //   //   this.pageSize +
    //   //   "&current=" +
    //   //   this.currentPage;
    //   let data = "size=" + this.pageSize + "&current=" + this.currentPage;
    //   coupon_list(data).then(result => {
    //     //console.log(result.data.data.records);

    //     if (result.data.status == 0) {
    //       let data = result.data.data.records;

    //       this.total = result.data.data.total;
    //       this.page = Math.ceil(result.data.data.total / this.pageSize);
    //       if (!data) {
    //         this.total = 0;
    //         this.page = 0;
    //       } else {
    //         data.forEach(item => {
    //           if (item.source == 1) {
    //             item.source = "手动创建";
    //           } else {
    //             item.source = "系统自动创建";
    //           }
    //           if (item.limitKind == 0) {
    //             item.startTime = "----------";
    //             item.endTime = "----------";
    //           }
    //           if (item.limitKind == 0) {
    //             item.limitKind = "无时间限制";
    //           } else {
    //             item.limitKind = "有时间限制";
    //           }
    //         });
    //         this.couponList = data;
    //       }
    //     }
    //   });
    // },
    addCoupon() {},
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = "id=" + id;
          coupon_delete(data).then(result => {
            // if (result.data.status == 0) {
            //   if (result.data.data == 1) {
            //     if (this.total % this.pageSize == 1) {
            //       if (this.currentPage != 1) {
            //         this.currentPage--;
            //       }
            //     }
            //     this.get_list();
            //     return this.$message({
            //       type: "success",
            //       message: "删除成功"
            //     });
            //   } else {
            //     return this.$message({
            //       type: "warning",
            //       message: "删除失败"
            //     });
            //   }
            // }
            if (this.total % this.pageSize == 1) {
              if (this.currentPage != 1) {
                this.currentPage--;
              }
            }
            this.get_list();
            return this.$message({
              type: "success",
              message: "删除成功"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    status_modify(id, status) {
      var goodsStatus = "";
      if (status == "1") {
        goodsStatus = 0;
      } else {
        goodsStatus = 1;
      }
      this.$confirm("此操作将修改状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.post("/web_coupon/modify" + id).then(result => {
            if (result.data.status == 0) {
              if (result.data.data == 1) {
                if (status == "0") {
                  this.$message({
                    type: "success",
                    message: "禁用成功"
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "启用成功"
                  });
                }
                this.couponList.forEach(item => {
                  if (item.id == id) {
                    item.status = item.status == "1" ? "0" : "1";
                  }
                });
              } else {
                this.$message({
                  type: "warning",
                  message: "操作失败"
                });
              }
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    verfy_startTime() {
      if (this.value5) {
        this.timeCompare(1);
      }
      if (this.value4 == null) {
        this.value4 = "";
      }
    },
    //   校验结束时间
    verfy_endtime() {
      if (this.value4) {
        this.timeCompare(2);
      }
      if (this.value5 == null) {
        this.value5 = "";
      }
    },

    timeCompare(flag) {
      //  flag  用于标记是清楚开始时间，还是清楚结束时间
      if (new Date(this.value4).getTime() >= new Date(this.value5).getTime()) {
        if (flag == 1) {
          this.value4 = "";
        } else {
          this.value5 = "";
        }
        return this.$message({
          type: "warning",
          message: "'优惠券创建时间-开始'必须小于'优惠券创建时间-结束'"
        });
      }
    },
    select_() {
      this.$router.push("/selectCouponType");
    }
  }
};
</script>


<style lang="less" >
.coupon_list-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  .content {
    .query {
      .start_time {
        display: flex;
        align-items: center;
      }
      .end_time {
        display: flex;
        align-items: center;
      }
      .btn {
        .el-button {
          width: 70px;
        }
      }
    }
  }
}
</style>